home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9893 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  51 lines

  1. Path: abacus.abasoft.co.uk!not-for-mail
  2. From: dmb@abacus.abasoft.co.uk (David Byrne)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Using cin like sscanf()?
  5. Date: 4 Mar 1996 17:20:12 -0000
  6. Organization: Abacus Software Ltd.
  7. Message-ID: <4hf8oc$47t@abacus.abasoft.co.uk>
  8. References: <4gnudd$dd4@daily-planet.nodak.edu>
  9. NNTP-Posting-Host: abacus.abasoft.co.uk
  10. X-NNTP-Posting-Host: abacus.demon.co.uk
  11.  
  12. In article <4gnudd$dd4@daily-planet.nodak.edu>,
  13. Jeffro <ishaq@plains.nodak.edu> wrote:
  14. >greetings!  I've always wondered if this is possible.  Well, I'm sure
  15. >it's possible, but how is it done?  Say I input a string into string:
  16. >
  17. >char string[] = "This is a character string.";
  18. >char buffer[24];
  19. >
  20. >How can I tell cin to read from string instead of stdin so I can do
  21. >soemthing like this:
  22. >
  23. >//cin.read_from(string)
  24. >while (cin >> buffer) cout << buffer;    //Outputs string
  25. >//cin.read_from(stdin)
  26.  
  27. Some day (maybe soon), I'm gonna be embarrassed by this attempt !!
  28. Anyway here goes...
  29.  
  30. #include <iostream.h>
  31. #include <strstream.h>
  32.  
  33. int main(void)
  34. {
  35.     char a[50];
  36.     char string[] = "This is a character string.";
  37.     istrstream i(string);
  38.     char c;
  39.  
  40.     do {
  41.         i >> a;
  42.         cout << a;
  43.     } while (!i.rdstate());
  44.     return 0;
  45. }
  46. -- 
  47. David Byrne, Abacus Software, London, UK              Tel: +44 (0)171 603 9877
  48. Email: dmb@abacus.demon.co.uk                         Fax: +44 (0)171 603 6844
  49. Here's a koan: If you have ice-cream I will give you some. If you have none,
  50.                I will take it away from you. (it's an ice-cream koan).
  51.